#include<stdio.h>
#include<stdbool.h>
#include "linkstack.h"
#include<stdlib.h>


//创建栈
linkstack stack_create(){
	linkstack s = (linkstack)malloc(sizeof(listnode));
	if(s == NULL){
		printf("init stack failed\n");
		return NULL;
	}
	s->data = 0;
	s->next = NULL;
	return s;
}
//往栈中放入一个元素
int stack_push(linkstack s, data_t value){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	linkstack p;
	if((p = (linkstack)malloc(sizeof(listnode))) == NULL){
		printf("create item failed\n");
		return -1;
	}
	p->data = value;
	p->next = s->next;
	s->next = p;
	return 0;

}
//从栈中拿出一个元素
data_t stack_pop(linkstack s){
	linkstack p;
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	if(s->next == NULL){
		printf("s is empty\n");
		return -1;
	}
	p = s->next;
	data_t data = p->data;
	s->next = p->next;
	free(p);
	return data;
}
//获取栈顶元素
data_t stack_top(linkstack s){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	if(s->next == NULL){
		printf("s is empty\n");
		return -1;	
	}
	return s->next->data;
	
}
//释放栈空间
int stack_free(linkstack s){
	linkstack p;
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	while(s != NULL){
		p = s->next;
		free(s);
		s = p;
	}
	return 0;
}
//打印栈列表
void stack_show(linkstack s){
	if(s == NULL){
		printf("s is NULL\n");
	}
	if(s->next == NULL){
		printf("s is empty\n");
	}
	while(s->next != NULL){
		printf("%d ", s->next->data);
		s = s->next;
	}
	puts("");
}
